const request = require('request');
let aes = require('./aes');
let rsa = require('./rsa');
let config = require('../config/config');

function getAccessToken() {
    return new Promise((resolve, reject) => {
        request.post({
            url: config.getTokenUrl,
            encoding: null,
            form: {devid: config.devId, devsecret: config.devSecret},
            json: true
        }, (err, response, body) => {
            if (!err) {
                resolve(body.data.access_token)
            }
        })
    })
}

function sign(source) {
    return rsa.sign(source, config.devRsaPrivateKey);
}

function verify(source, signature) {
    return rsa.verify(source, signature, config.rsaPublicKey);
}

function encText(text) {
    return aes.encText(text, config.aesKey);
}

function decText(text_encrypt) {
    return aes.decText(text_encrypt, config.aesKey);
}

module.exports = {getAccessToken, sign, verify, encText, decText};
